import java.awt.*;

import javax.swing.*;

public class UnitsLayer extends JPanel {
	Cursor cursor;
	Player[] players;
	String[] imageFile = { "army.png", "explorer.png", "colonist.png",
			"mele.png", "range.png" };
	Image[] image = new Image[imageFile.length];

	public UnitsLayer(Cursor cursor, Player[] players) {
		this.cursor = cursor;
		this.players = players;
		this.setFocusable(false);

		// Initialize images from their file names
		MediaTracker media = new MediaTracker(this);
		for (int i = 0; i < 4; i++) {
			image[i] = Toolkit.getDefaultToolkit().getImage(
					"graphics\\" + imageFile[i]);
			media.addImage(image[i], 0);
			try {
				media.waitForID(0);
			} catch (Exception e) {
			}

		}
	}

	public void paint(Graphics g) {
		this.setLocation(0, 0);
		int w = 850, h = 550;
		this.setSize(w, h);

		int posx, posy;
		posx = -8 + cursor.getx();
		posy = -5 + cursor.gety();
		Tileable[][][] tileables = players[0].getTileables();
		Tileable[][] units = tileables[0];
		Tileable[] rallys = tileables[2][0];
		int[][] sizes = players[0].getArrayOfSizes();

		// Populate map with rally images
		for (int i = 0; i < sizes[2][0]; i++) {
			if (rallys[i] != null) {
				int x = (rallys[i].getX() - posx);
				int y = (rallys[i].getY() - posy);
				if ((x < 0) || (x > 16) || (y < 0) || (y > 16))

				{
					// Outside of areaView bounds
				} else {
					// Draw rally image
					g.drawImage(image[0], x * 50, y * 50, this);
				}
			}
		}

		// Populate the map with unit images
		for (int i = 0; i < units.length; i++) {
			if (units[i] == null) {

			} else {
				for (int j = 0; j < sizes[0][i]; j++) {
					if (units[i][j] == null) {

					}
					// Draw unit image
					else {
						int x = (units[i][j].getX() - posx);
						int y = (units[i][j].getY() - posy);
						if ((x < 0) || (x > 16) || (y < 0) || (y > 16))

						{
							// Outside of areaView bounds
						} else {
							// System.out.println("////// "+units[i][j].getStatus()[1]+" \\\\\\");
							// Draw unit image
							g.drawImage(image[units[i][j].getStatus()[1] + 1],
									x * 50, y * 50, this);
						}
					}
				}
			}
		}

	}
}
